package com.doupi.partner.manager;

import com.doupi.partner.dal.model.Administrator;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.service.AdministratorService;
import com.doupi.partner.service.PartnerRelationService;
import com.doupi.partner.service.SystemConfigService;
import com.doupi.partner.service.dto.PartnerRelationDto;
import com.doupi.partner.util.ObjectUtils;
import com.doupi.partner.util.RuntimeBusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 合伙人关系 manager
 * 
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-04-16
 */
@Component
public class PartnerRelationManager {
	private final AdministratorService administratorService;

	private final SystemConfigService systemConfigService;

	private final AdministratorManager administratorManager;

	private final PartnerRelationService partnerRelationService;

	@Autowired
	public PartnerRelationManager(AdministratorService administratorService, SystemConfigService systemConfigService, AdministratorManager administratorManager,
			PartnerRelationService partnerRelationService) {
		this.administratorService = administratorService;
		this.systemConfigService = systemConfigService;
		this.administratorManager = administratorManager;
		this.partnerRelationService = partnerRelationService;
	}

	/**
	 * 将List<PartnerRelation>转为 List<PartnerRelationDto> lists
	 */
	public List<PartnerRelation> convertPartnerRelationListToDtoList(List<PartnerRelation> records) {
		List<PartnerRelation> lists = new ArrayList<>();
		for (PartnerRelation partner : records) {
			lists.add(convertPartnerRelationToDto(partner));
		}
		return lists;
	}

	/**
	 * 将PartnerRelation转为PartnerRelationDto
	 */
	public PartnerRelationDto convertPartnerRelationToDto(PartnerRelation partnerRelation) {
		PartnerRelationDto partnerRelationDto = new PartnerRelationDto();
		ObjectUtils.copyProperties(partnerRelationDto, partnerRelation);
		partnerRelationDto.setAdministrator(administratorService.selectByPrimaryKey(partnerRelation.getAdministratorId()));
		partnerRelationDto.setSuperiorAdministrator(administratorService.selectByPrimaryKey(partnerRelation.getSuperiorAdministratorId()));
		partnerRelationDto.setCityAdministrator(administratorService.selectByPrimaryKey(partnerRelation.getCityAdministratorId()));
		partnerRelationDto.setSubstationAdministrator(administratorService.selectByPrimaryKey(partnerRelation.getSubstationAdministratorId()));
		partnerRelationDto.setCityConfig(systemConfigService.selectByPrimaryKey(partnerRelationDto.getCity()));
		return partnerRelationDto;
	}

	/**
	 * 保存合伙人关系，同时关联管理员账号
	 * 
	 * @param mobile
	 *            手机号
	 * @param name
	 *            姓名
	 * @param partnerRelation
	 *            关系
	 * @return 影响记录数
	 */
	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public int savePartnerRelation(String mobile, String name, PartnerRelation partnerRelation, String password) {
		Administrator administrator = administratorService.selectByMobile(mobile);
		if (null != administrator && !administrator.getId().equals(partnerRelation.getAdministratorId())) {
			throw RuntimeBusinessException.createException("手机号已经存在");
		}
		if (null == partnerRelation.getAdministratorId()) { // 没有管理员账号，初始化一个
			partnerRelation.setMobile(mobile);
			partnerRelation.setName(name);
			administrator = administratorManager.initAdministrator(mobile, name, "partner_" + partnerRelation.getType(), password);
			partnerRelation.setAdministratorId(administrator.getId());
		} else { // 修改管理员账号信息
			administratorService.updateAdministratorNameAndMobile(partnerRelation.getAdministratorId(), name, mobile);
		}
		return partnerRelationService.savePartnerRelation(partnerRelation);
	}
}
